﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

using Might.Infrastructure;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.ModelicaDOM;

using Microsoft.Windows.Controls;

using Microsoft.Practices.Unity;
using Might.Infrastructure.Services;
using Might.Infrastructure.DOM;
using Might.GraphicsEditor.Presenter;

namespace Might.UIControls.PackageBrowser {
    /// <summary>
    /// Treeview component.
    /// </summary>
    public partial class PackageBrowserView : IPackageBrowserView {            
    
        private readonly PackageBrowserPresenter presenter;                        
        private readonly MouseDoubleClickManager doubleClickManager;
        private readonly IUnityContainer container;

        /// <summary>
        /// Initializes a new instance of the <see cref="TreeView"/> class.
        /// </summary>
        /// <param name="presenter">The presenter</param>
        /// <param name="container"></param>
        public PackageBrowserView( PackageBrowserPresenter presenter , IUnityContainer container ) {
            
            InitializeComponent();

            doubleClickManager = new MouseDoubleClickManager( ModelTree );
            doubleClickManager.DoubleClick += doubleClickManager_DoubleClick;
            
            this.container = container;
            this.presenter = presenter;
            this.presenter.View = this;                       
                                   
        }

        ///<summary>
        ///</summary>
        public event EventHandler ModelTreeDoubleClick;

        public object SelectedItemData {
            get {
                if( ModelTree.SelectedItem != null ) {
                    return ( (TreeViewItem)ModelTree.SelectedItem ).Tag;
                }
                return null;
            }
        }  
        
        public void AddTreeRoot( TreeViewItem root ) {            
            addTreeItems( root , ModelTree.Items );
        }

        public void AddTreeItem( TreeViewItem item , TreeViewItem toItem ) {
            addTreeItems( item , toItem.Items );
        }   
        
        public void UpdateIcon( TreeViewItem item ) {            
            createIconPresenter( item , item.Tag as IDomNode ); 
        }    

        public void Clear() {
            ModelTree.Items.Clear();
        }        

        private void addTreeItems( TreeViewItem item , ItemCollection toCollection ) {

            item.MouseLeftButtonUp += doubleClickManager.HandleClick;
            item.IsExpanded = true;           
                        
            createIconPresenter( item , item.Tag as IDomNode );

            toCollection.Add( item );

            foreach( TreeViewItem childItem in item.Items ) {
                addTreeItems( childItem , item.Items );
            }  
            
        }      
                   
        private void createIconPresenter( TreeViewItem item , IDomNode node ) {

            var stack = new StackPanel { Orientation = Orientation.Horizontal };
            if(  node is IModelicaClass ) {
                stack.Children.Add( new TextBlock { Text = ( node as IModelicaClass ).ClassName } );
            }
            
            IGraphicsAnnotation annotation = container.Resolve<IIconProvider>().GetGraphicsAnnotation( node );
            
            if( annotation != null ) {            
                var icon = new IconPresenter( annotation ) { Height = 20, Width = 20 };
                stack.Children.Add( icon );
            }                        
            
            item.Header = stack;
            
        }               
        
        protected void onModelTreeDoubleClick( object sender , EventArgs e ) {
            if( ModelTreeDoubleClick != null ) {
                ModelTreeDoubleClick( this , e );
            }
        }

        void doubleClickManager_DoubleClick( object sender , MouseButtonEventArgs e ) {            
            if( ModelTree.SelectedItem != null ) {                                                            
                onModelTreeDoubleClick( sender , e );
                e.Handled = true;                
            }            
        }    

    }
}